<template>
    <div class="app-container">
        <applyDetails v-if="form" :priceFlag="priceFlag" :taskName='taskName' :configure="configure" :form="form" />
    </div>
</template>
<script>
import applyDetails from "../todo/addCTodoNoXc.vue"
import { ResourcesChange } from "@/api/todoItem/todo.js";
import {
    serviceByClazz,
    classByCode,
} from "@/api/resources/apply.js";
import { getDicts } from "@/api/system/dict/data";
export default {
    components:{
        applyDetails
    },
    data(){
        return{
            priceFlag:'',
            taskName:'',
            configure:{
              storage_type:[],
              ccList:[],
              fwcpList:[],
              codeList:[],
            },
            form:null,
            loading:null,
        }
    },
    created(){
      this.loading = this.$loading({
                lock: true,
                text: '加载中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
              });
        this.OnLoder()
    },
    methods:{
        OnLoder(){
            var list = JSON.parse(sessionStorage.getItem('ChangeapplyDetailsNoXc'));
            this.priceFlag = false;
            var ifStatistic = "false";
            ResourcesChange({ifStatistic, id: list.processInstanceId}).then(res=>{
                this.getDataOfAdd(res.data.cloudSupplier,res.data,ifStatistic,list)
            })
        },
        //获取一些配置数据
        getDataOfAdd(data,formdata,ifStatistic,list) {
            if(this.funNumber >1){

            }else{
                var detail = formdata
                //可用域
                getDicts("available_domain").then((response) => {
                    this.configure.available_domain = response.data;
                });
                //服务商
                getDicts("cloud_service_provider").then((response) => {
                    this.configure.cloud_service_provider = response.data;
                });
                //盘类型
                serviceByClazz({ code: "cc" ,supplier:data}).then((response) => {
                    response.data.forEach((item) => {
                        if (
                            ["FCcc(tykcc)2", "FCcc_ssd", "spycc2"].indexOf(
                                item.code
                            ) < 0
                        ) {
                            this.configure.storage_type.push(item);
                        }
                        if (["spycc2"].indexOf(item.code) < 0) {
                            this.configure.ccList.push(item);
                        }
                        if (
                            ["fbscc_wjcc2", "FCcc(tykcc)2", "FCcc_ssd"].indexOf(
                                item.code
                            ) < 0
                        ) {
                            this.configure.fwcpList.push(item);
                        }
                    });
                });
                //操作系统
                classByCode({ code: "czxt" }).then((response) => {
                    this.configure.czxtList = response.data;
                });
                // //云服务器规格列表
                // listByCode("spec_ecs").then((response) => {
                //     this.configure.codeList = response.data;
                // });
                //高性能服务器资源类型列表
                serviceByClazz({ code: "wlfwq",supplier:data }).then((response) => {
                    this.configure.wlfwqList = response.data;
                });
                //数据库类型列表
                serviceByClazz({ code: "sjk",supplier:data }).then((response) => {
                    this.configure.sjkList = response.data;
                });
                //安全产品列表
                serviceByClazz({ code: "yyaq",supplier:data }).then((response) => {
                    this.configure.yyaqList = response.data;
                });
                setTimeout(() => {
                    this.configure.cloud_service_provider.some((item) => {
                        if (item.dictValue == detail.cloudSupplier) {
                            detail.cloudSupplier_label = item.dictLabel;
                            return true;
                        }
                    });
                    detail.storeProducts.forEach((data) => {
                        this.configure[
                            data.productCode == "共享型" ? "ccList" : "fwcpList"
                        ].some((it) => {
                            if (it.code == data.diskServerCode) {
                                data.diskServerCode_label = it.serviceName;
                                return true;
                            }
                        });
                        data.hostInstanceNames = [];
                        if (data.hosts) {
                            data.hosts.forEach((item) => {
                                data.hostInstanceNames.push(item.instanceName);
                            });
                        }
                    });
                    detail.databases.forEach((data) => {
                        this.configure.sjkList.some((it) => {
                            if (it.code == data.serverCode) {
                                data.serverCode_label = it.serviceName;
                                return true;
                            }
                        });
                    });
                    detail.hosts.forEach((data) => {
                        data.hostName = data.instanceName;
                        //可用域
                        this.configure.available_domain.some((item) => {
                            if (item.dictValue == data.availableDomain) {
                                data.availableDomain_label = item.dictLabel;
                                return true;
                            }
                        });
                        //数据盘类型
                        if (data.disks && data.disks.length) {
                            data.disks.forEach((item) => {
                                this.configure.ccList.some((it) => {
                                    if (it.code == item.diskServerCode) {
                                        item.diskServerCode_label = it.serviceName;
                                        return true;
                                    }
                                });
                            });
                        } else {
                            data.disks = [];
                        }
                        if (data.flavorType == 1) {
                            //系统盘类型
                            if (data.osDisk) {
                                this.configure.storage_type.some((item) => {
                                    if (item.code == data.osDisk.diskServerCode) {
                                        data.osDisk.diskServerCode_label =
                                            item.serviceName;
                                        return true;
                                    }
                                });
                            }
                            this.configure.codeList.some((item) => {
                                if (item.specCode == data.cloudFlavorCode) {
                                    data.cloudFlavorCode_label = item.specName;
                                    data.cloudFlavorCode_desc = item.desc;
                                    return true;
                                }
                            });
                        }
                        if (data.flavorType == 2) {
                            this.configure.wlfwqList.some((item) => {
                                if (item.code == data.cloudFlavorCode) {
                                    data.cloudFlavorCode_label = item.serviceName;
                                    data.cloudFlavorCode_desc = item.remark;
                                    return true;
                                }
                            });
                        }
                    });
                    setTimeout(() => {
                        detail.safeProductsList=[];
                        let  productNames=[];
                        detail.safeProducts.forEach(item=>{
                          if(productNames.indexOf(item.productName)<0){
                            productNames.push(item.productName)
                            let num=0
                            if(!item.changeTo||item.changeTo.insStatus!='REMOVE'){
                              num=1
                            }
                            detail.safeProductsList.push({name:item.productName,num,ysnum:1})
                          }else{
                            if(!item.changeTo||item.changeTo.insStatus!='REMOVE'){
                              detail.safeProductsList[productNames.indexOf(item.productName)].num++
                            }
                            detail.safeProductsList[productNames.indexOf(item.productName)].ysnum++
                          }
                        })
                        detail.hosts.forEach((item,index)=>{
                            let changeItem=item.changeTo,
                                changeDiskFlag=false;
                              if(changeItem){
                                  if(item.disks.length!=changeItem.length){
                                    changeDiskFlag=true
                                  }else{
                                      item.disks.some((it,inde)=>{
                                          let flag=true
                                          changeItem.disks.some((i,ind)=>{
                                              if(it.diskServerName==i.diskServerName&&it.dataDiskSize==i.dataDiskSize){
                                                flag=false
                                                changeItem.splice(ind,1)
                                                return true
                                              }
                                          })
                                          changeDiskFlag=flag
                                          if(flag){
                                            return true
                                          }
                                      })
                                  }
                              }
                              detail.hosts[index].changeDiskFlag=changeDiskFlag
                        })

                        console.log(detail,'xxxxxxxxxxxxxxxx')

                        this.form = detail;
                        this.form.ifStatistic =
                            ifStatistic == "true" ? true : false;
                        this.open = true;
                        this.loading.close()
                    }, 200);
                }, 1000);
            }
        },
    }
}
</script>
